package com.b1502.store2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.model.HomeProductDetailsBean;
import com.b1502.store2.network.PostParams;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.xutils.common.Callback;
import org.xutils.x;

/**
 * 产品详情页面
 */
public class ProductDetailActivity extends BaseActivity {

    private PercentLinearLayout mproduct_details_top;
    private ImageView mproduct_details_show_image;
    private TextView mproduct_details_nameTv;
    private TextView mproduct_details_category;
    private TextView mproduct_details_price_num;
    private TextView product_details_already_check;
    private RelativeLayout product_details_send_rela;
    private TextView mproduct_details_send;
    private TextView mproduct_details_send_condition;
    private RelativeLayout mproduct_details_rodute_rel;
    private TextView mproduct_details_redute;
    private TextView mproduct_details_redute_condition;
    private PercentLinearLayout mproduct_details_bottom;
    private TextView mproduct_details_commodity_message;
    private RelativeLayout product_details_nameMsg;
    private TextView mproduct_details_name;
    private TextView mproduct_details_nameTTv;
    private RelativeLayout mproduct_details_burdenMsg;
    private TextView mproduct_details_burden;
    private TextView product_Details_burdenTv;
    private RelativeLayout mproduct_details_producMsg;
    private TextView mproduct_details_produc;
    private TextView mproduct_details_producTv;
    private RelativeLayout mproduct_details_gradeMsg;
    private TextView mproduct_details_grade;
    private TextView mproduct_details_gradeTv;
    private RelativeLayout mproduct_details_eatMsg;
    private TextView mproduct_details_eat;
    private TextView mproduct_details_eatTv;
    private RelativeLayout mproduct_details_storeMsg;
    private TextView mproduct_details_store;
    private TextView product_details_storeTv;
    private ImageView mto_shopcar;
    private Button add_to_shopcar;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        initStoreBar("商品详情");
        initView();
        gson = new Gson();
        Intent intent = getIntent();
        if (intent != null) {
            String productId = intent.getStringExtra("productId");
            getProduct("productId", productId);
            addItem(1, productId);
        }
    }
    //初始化资源id
    private void initView() {
        //上方的整体布局
        mproduct_details_top = (PercentLinearLayout) findViewById(R.id.product_Details_top);
        //展示在上方的图片
        mproduct_details_show_image = (ImageView) findViewById(R.id.product_Details_show_image);
        //商品名称
        mproduct_details_nameTv = (TextView) findViewById(R.id.product_Details_nameTv);
        //产品类别
        mproduct_details_category = (TextView) findViewById(R.id.product_Details_category);
        //价钱数量
        mproduct_details_price_num = (TextView) findViewById(R.id.product_Details_price_num);
        //选中的产品件数
        product_details_already_check = (TextView) findViewById(R.id.product_Details_already_check);
        //满送的整体布局
        product_details_send_rela = (RelativeLayout) findViewById(R.id.product_Details_send_rela);
        //满赠
        mproduct_details_send = (TextView) findViewById(R.id.product_Details_send);
        //满赠的信息
        mproduct_details_send_condition = (TextView) findViewById(R.id.product_Details_send_condition);
        //满减的整体布局
        mproduct_details_rodute_rel = (RelativeLayout) findViewById(R.id.product_Details_rodute_Rel);
        //满减
        mproduct_details_redute = (TextView) findViewById(R.id.product_Details_redute);
        //满减的信息
        mproduct_details_redute_condition = (TextView) findViewById(R.id.product_Details_redute_condition);
        //下方的整体布局
        mproduct_details_bottom = (PercentLinearLayout) findViewById(R.id.product_Details_bottom);
        //商品信息的字段
        mproduct_details_commodity_message = (TextView) findViewById(R.id.product_Details_commodity_message);
        //包含产品名称的布局
        product_details_nameMsg = (RelativeLayout) findViewById(R.id.product_Details_nameMsg);
        //产品名称的字段
        mproduct_details_name = (TextView) findViewById(R.id.product_Details_name);
        //产品名称的信息
        mproduct_details_nameTTv = (TextView) findViewById(R.id.product_Details_nameTTv);
        //配料的整体布局
        mproduct_details_burdenMsg = (RelativeLayout) findViewById(R.id.product_Details_burdenMsg);
        //配料
        mproduct_details_burden = (TextView) findViewById(R.id.product_Details_burden);
        //配料的详细内容
        //product_Details_burdenTv = (TextView) findViewById(product_Details_burdenTv);
        //产地的整体布局
        mproduct_details_producMsg = (RelativeLayout) findViewById(R.id.product_Details_producMsg);
        //产地的字段
        mproduct_details_produc = (TextView) findViewById(R.id.product_Details_produc);
        //产地的详细内容
        mproduct_details_producTv = (TextView) findViewById(R.id.product_Details_producTv);
        //等级的整体布局
        mproduct_details_gradeMsg = (RelativeLayout) findViewById(R.id.product_Details_gradeMsg);
        //等级
        mproduct_details_grade = (TextView) findViewById(R.id.product_Details_grade);
        //等级具体的信息
        mproduct_details_gradeTv = (TextView) findViewById(R.id.product_Details_gradeTv);
        //使用方法的整体布局
        mproduct_details_eatMsg = (RelativeLayout) findViewById(R.id.product_Details_eatMsg);
        //食用方法的字段
        mproduct_details_eat = (TextView) findViewById(R.id.product_Details_eat);
        //食用方法的具体信息
        mproduct_details_eatTv = (TextView) findViewById(R.id.product_Details_eatTv);
        //贮藏方法的布局
        mproduct_details_storeMsg = (RelativeLayout) findViewById(R.id.product_Details_storeMsg);
        //贮藏方法
        mproduct_details_store = (TextView) findViewById(R.id.product_Details_store);
        //贮藏方法的具体信息
        product_details_storeTv = (TextView) findViewById(R.id.product_Details_storeTv);
        //最下方的购物车图标
        mto_shopcar = (ImageView) findViewById(R.id.to_shopcar);
        //加入购物车的按钮
        add_to_shopcar = (Button) findViewById(R.id.add_to_shopcar);
    }

    //商品详情信息
    private void getProduct(String param, String productId) {
        StoreParams params = new StoreParams(UrlUtil.GetProduct, param,
                productId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onSuccess(String result) {
                HomeProductDetailsBean homeProductDetailsBean = gson.fromJson(result, HomeProductDetailsBean.class);
                if(homeProductDetailsBean!=null){
                    Glide.with(ProductDetailActivity.this).load(homeProductDetailsBean.getImageUrl()).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mproduct_details_show_image);
                    mproduct_details_nameTv.setText(homeProductDetailsBean.getName());
                    //商品类别
                    mproduct_details_category.setText("类别："+homeProductDetailsBean.getProductCategoryName());
                    //价钱
                    mproduct_details_price_num.setText("¥"+homeProductDetailsBean.getMemberPrice());
                    //满赠
                    mproduct_details_send_condition.setText(homeProductDetailsBean.getPromotions().get(0).getContents().get(0).getDescription());
                    //满减
                    mproduct_details_redute.setText(homeProductDetailsBean.getPromotions().get(1).getContents().get(0).getDescription());
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 添加到购物车
     * @param count     购买商品数量
     * @param productId 购买商品ID
     */
    private void addItem(int count, String productId) {
        StoreParams params = new StoreParams(UrlUtil.AddItem);
        params.setAsJsonContent(true);
        PostParams postParams = new PostParams(count, productId);
        params.setBodyContent(new Gson().toJson(postParams));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                Log.d(TAG, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
